package com.ruoyi.web.core.config;

import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * Created by Lxxxxxxy_ on 2022/11/21 10:38.
 */
@Component
public class WxConfig {
    private static Integer agentId;

    private static String secret;

    private static String corpId;

    private static String msgType;

    private static String safe;

    private static String url;

    @Value("${wx.agentId}")
    public void setAgentId(Integer agentId) {
        this.agentId = agentId;
    }

    @Value("${wx.secret}")
    public void setSecret(String secret) {
        this.secret = secret;
    }

    @Value("${wx.corpId}")
    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }

    @Value("${wx.msgType}")
    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    @Value("${wx.safe}")
    public void setSafe(String safe) {
        this.safe = safe;
    }

    @Value("${wx.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    // 配置企业微信服务
    public static WxCpService getWxCpService() {
        WxCpService wxCpService = new WxCpServiceImpl();
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setAgentId(agentId);
        config.setCorpSecret(secret);
        config.setCorpId(corpId);
        wxCpService.setWxCpConfigStorage(config);
        return wxCpService;
    }

    public static WxCpMessage getWxCpMessage() {
        WxCpMessage wxCpMessage = new WxCpMessage();
        /**
         * 表示是否是保密消息，默认为0；注意仅 mpnews 类型的消息支持safe值为2，其他消息类型不支持
         * 0表示可对外分享
         * 1表示不能分享且内容显示水印
         * 2表示仅限在企业内分享
         */
        wxCpMessage.setSafe(safe);
        /**
         * 消息类型
         * 文本消息: text
         * 图片消息: image
         * 语音消息: voice
         * 视频消息: video
         * 文件消息: file
         * 文本卡片消息: textcard
         * 图文消息: news
         * 图文消息: mpnews
         * markdown消息: markdown
         * 模板卡片消息: template_card
         */
        wxCpMessage.setMsgType(msgType);
        /**
         * 点击后跳转的链接。最长2048字节，请确保包含了协议头(http/https)
         */
        wxCpMessage.setUrl(url);
        return wxCpMessage;
    }
}